using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;
using System.Runtime.InteropServices;

namespace DriveLocker
{
    public partial class FrmDriveLocker : Form
    {
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern bool SendNotifyMessage(IntPtr hWnd, uint Msg, IntPtr wParam,
           IntPtr lParam);

        public FrmDriveLocker()
        {
            InitializeComponent();
            this.Icon = global::DriveLocker.Properties.Resources.DriveAndLock;
            DriveInfo[] drives = DriveInfo.GetDrives();
            RegistryKey regkey = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\Explorer");
            int val=0;
            int.TryParse(regkey.GetValue("NoDrives", "0").ToString(), out val);

            foreach (DriveInfo drive in drives)
            {
                if (drive.DriveType == DriveType.Fixed)
                {
                    ListViewItem li = new ListViewItem();
                    string driveLabel = drive.VolumeLabel;
                    string driveName = drive.Name;
                    driveName = driveName.TrimEnd('\\');
                    if(string.IsNullOrEmpty(driveLabel)) driveLabel="Local Disk";
                    li.Text = string.Format("{0} ({1})", driveLabel, driveName);
                    int driveVal = ((int)driveName.ToUpper()[0]) - 65;
                    li.Tag = driveVal;
                    li.Checked = ((val>>driveVal) & 1)== 1;
                    li.ImageKey = "drive";
                    listView1.Items.Add(li);
                }
            }
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            int val = 0;
            foreach (ListViewItem li in listView1.CheckedItems)
            {
                int driveId = (int)li.Tag;
                val += (int)Math.Pow(2, driveId);
            }
            RegistryKey regkey = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\Explorer");
            if (val > 0)
            {
                regkey.SetValue("NoDrives", val, RegistryValueKind.DWord);
                regkey.SetValue("NoViewOnDrive", val, RegistryValueKind.DWord);
            }
            else
            {
                regkey.DeleteValue("NoDrives", false);
                regkey.DeleteValue("NoViewOnDrive", false);
            }
            regkey.Close();
            regkey = null;
            IntPtr hwnd = new IntPtr(0xFFFF);
            IntPtr wparam = new IntPtr(0);
            IntPtr lparam = Marshal.StringToHGlobalUni("Policy");
            SendNotifyMessage(hwnd, 0x1A, wparam, lparam);
            System.Threading.Thread.Sleep(1500);
            Marshal.FreeHGlobal(lparam);
            lparam = new IntPtr(0);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(linkLabel1.Text);
        }
    }
}